/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package privatemessanger;

import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Elad
 */
public class Statistics {
    private static final String DATE_FORMAT_NOW ="HH:mm,dd/MM/yy"; // the date format.
    private File file;
    private String Line=null;
    private String maxDate=null, serverName=null;
     private String PATH = "statistics/";
      private String SFX = ".txt";
     private  BufferedWriter out;
     private int totalMessages=0, maxClients=1, totalClients=1;
     private int currentUsers=1;

     public Statistics()
     {
        serverName=MainFrame._user.getServerArray().get(0).getServerName();
           Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            String time=sdf.format(cal.getTime());
            maxDate=time;
        read();
     }
     public void read()
    {
         StringBuilder text = new StringBuilder();
         String NL = System.getProperty("line.seperator");
       try {
           file= new File(PATH+serverName+SFX);
           if(file.exists())
           {
           Scanner scanner = new Scanner(file,"UTF-8");
           if(scanner.hasNextLine())
               {
                    // this statement reads the line from the file
               while(scanner.hasNextLine())
                        parser(scanner.nextLine());

                    // dispose all the resources after using them.
                     scanner.close();
                }
              }
            }
                    catch (FileNotFoundException e) { e.printStackTrace();}
                   catch (IOException e) {e.printStackTrace();}
    }
     public void write()
     {
         if (file.exists())
             file.delete();

         //create the file
         try {
                new File(PATH).mkdir();
            } catch (Exception e){} //Catch exception if any
        try {
            // Create file
           out = new BufferedWriter(new FileWriter(PATH+serverName+SFX,true));
        } catch (Exception e){ System.err.println("Error: " + e.getMessage()); }  //Catch exception if any

         //write to the file
         try {
            out.write("MaxClients: "+maxClients+" "+maxDate);
            out.newLine();
            out.write("TotalMessages: "+totalMessages);
            out.newLine();
            out.write("TotalClients: "+totalClients);
            out.newLine();
            out.close();
        } catch (IOException ex) { Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex); }
     }
     private void parser(String s)
     {
         String[] _array=s.split(" ");
         if(_array[0].equals( "MaxClients:"))
         {
             maxClients= Integer.valueOf(_array[1]).intValue();
             maxDate= _array[2];
         }
          if(_array[0].equals( "TotalClients:"))
                totalClients=Integer.valueOf(_array[1]).intValue();
          if(_array[0].equals( "TotalMessages:"))
             totalMessages= Integer.valueOf(_array[1]).intValue();
     }
     public void setCurrentUsers(boolean bool)
     {
         if (bool == true)
         {
             currentUsers++;
             totalClients++;
             if(currentUsers >= maxClients)
                 MaxUsers();
         }
         else currentUsers--;
     }
     private void MaxUsers()
     {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            String time=sdf.format(cal.getTime());
            maxDate=time;
            maxClients=currentUsers;
     }
     public void setMessageCounter()
     {
         totalMessages++;
     }


     public int getTotalMessages(){return totalMessages;}
     public int getMaxClients(){return maxClients;}
     public String getMaxClientsDate(){return maxDate;}
     public int getTotalClients(){return totalClients;}
     public String getName(){return serverName;}
}
